package com.juhuixing.user.server.controller;

import com.juhuixing.user.server.business.service.UserService;
import com.juhuixing.user.server.persistence.entity.BaseUser;
import com.juhuixing.common.base.BaseController;
import com.juhuixing.common.base.ResponseData;
import com.juhuixing.common.dto.UserDetailDTO;
import com.juhuixing.common.enums.LoginEnum;
import com.juhuixing.common.model.TokenData;
import com.juhuixing.common.utils.custom.PhoneFormatCheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @return:
 * @author: 刘猛
 * @date: 2019/2/20 16:44
 **/
@RestController
@Api(tags = "用户相关信息API")
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @ApiOperation("通过用户名获取用户信息")
    @GetMapping("/findByUsername")
    public ResponseData<BaseUser> findByUsername(@RequestParam("username") String username){
        BaseUser user = userService.findByUsername(username);
        if (user == null){
            return ResponseData.fail("用户不存在",100);
        }
        return ResponseData.ok(user);
    }

    /**
     * 功能描述: 获取当前用户信息
     * @param: []
     * @return: com.juhuixing.common.base.ResponseData
     * @author: 刘猛
     * @date: 2019/3/24 9:47
     **/
    @ApiOperation("获取用户详细信息")
    @GetMapping("/getUserDetail")
    public ResponseData<UserDetailDTO> getUserDetail(){
        return ResponseData.ok(userService.getUserDetail(super.getCurrentUserName()));
    }

    /**
     * 功能描述: 用户注册登录
     * @return: com.juhuixing.common.base.ResponseData
     * @author: 刘猛
     * @date: 2019/3/19 17:05
     **/
    @ApiOperation(value = "用户注册登录",notes = "如果是微信登录方式，请先调用/api/user/checkWeChatRegisted接口判断是否是老用户登录，老用户登录直接返回token不要再调用此接口")
    @PostMapping("/registerAndLogin/{way}")
    public ResponseData<TokenData> register(
                            @PathVariable("way") @NotBlank @ApiParam(value="登陆方式，手机 phone 微信 wechat",required = true) String way
                            ,@RequestParam(required = false) @ApiParam(value="手机号") String phone
                            ,@RequestParam(required = false) @ApiParam(value="短信验证码") String smsCode
                            ,@RequestParam(required = false) @ApiParam(value="头像") String avatar
                            ,@RequestParam(required = false) @ApiParam(value="用户昵称") String nickName
                            ,@RequestParam(required = false) @ApiParam(value="宠物昵称") String petNickName
                            ,@RequestParam(required = false) @ApiParam(value="性别：男 1，女 0") Integer sex
                            ,@RequestParam(required = false) @ApiParam(value="生日") String birthday
                            ,@RequestParam(required = false) @ApiParam(value="微信openId") String openId
                            ,@RequestParam(required = false) @ApiParam(value="微信unionId") String unionId
                            ,@RequestParam(required = false) @ApiParam(value="宠物形象图片链接地址") String petImage
                            ,@RequestParam(required = false) @ApiParam(value="宠物形象背景图链接地址") String backImage
                            ,@RequestParam(required = false) @ApiParam(value="语音链接地址") String voiceIntro
                            ,@RequestParam(required = false) @ApiParam(value="宠物配音") String petVoice
                            ,@RequestParam(required = false) @ApiParam(value="宠物类型") Integer petType

                            /**微信用户信息**/
                            ,@RequestParam(required = false) @ApiParam(value="微信头像") String wxavatar
                            ,@RequestParam(required = false) @ApiParam(value="微信昵称") String wxnickname
                            ,@RequestParam(required = false) @ApiParam(value="微信性别") Integer gender
                            ,@RequestParam(required = false) @ApiParam(value="微信省") String province
                            ,@RequestParam(required = false) @ApiParam(value="微信城市") String city

    ){
        TokenData token = null;
        if(StringUtils.equalsIgnoreCase(LoginEnum.PHONE.getType(),way)){
            /**手机号登录**/
            token = userService.phoneLogin(phone,smsCode,super.getSource());
        }else if(StringUtils.equalsIgnoreCase(LoginEnum.WECHAT.getType(),way)){
            token = userService.wechatLogin(
                     phone,
                     avatar,
                     nickName,
                     petNickName,
                     sex,
                     birthday,
                     openId,
                     unionId,
                     petImage,
                     backImage,
                     voiceIntro,
                     petVoice,
                     petType,
                     super.getSource(),
                     wxavatar,
                     wxnickname,
                     gender,
                     province,
                     city
                    );
        }
        return ResponseData.ok(token);
    }


    /**
     * 功能描述: 账号秘密登录
     * @return: com.juhuixing.common.base.ResponseData
     * @author: 刘猛
     * @date: 2019/3/19 17:05
     **/
    @ApiOperation(value = "账号密码登录")
    @PostMapping("/pwdLogin")
    public ResponseData<TokenData> pwdLogin(@RequestParam @ApiParam(value="账号") String username
                                            ,@RequestParam @ApiParam(value="密码") String password
    ){
        return ResponseData.ok(userService.pwdLogin(username,password));
    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @ApiOperation("短信发送")
    @GetMapping("/send/verification/code")
    public ResponseData<String> sendCode(@RequestParam @NotBlank @ApiParam(value = "手机号",required = true) String phone) {
        //判断手机号格式
        if (!PhoneFormatCheckUtils.isPhoneLegal(phone)) {
            return ResponseData.fail("手机号格式错误");
        }
        try {
            return  ResponseData.ok(userService.createSmsCode(phone));
        } catch (Exception e) {
            log.error("验证码发送失败,手机号:{}",phone);
            return ResponseData.fail("验证码发送失败");
        }
    }
}
